package com.recruit.wechat.serviceImpl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.recruit.wechat.bean.Role;
import com.recruit.wechat.bean.RolePermission;
import com.recruit.wechat.bean.result.ResultPage;
import com.recruit.wechat.controller.helper.$;
import com.recruit.wechat.controller.model.ApiResultModel;
import com.recruit.wechat.mapper.RoleMapper;
import com.recruit.wechat.service.RolePermissionService;
import com.recruit.wechat.service.RoleService;
import com.recruit.wechat.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 肖守凯
 * @since 2019-09-03
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    RolePermissionService rolePermissionService;
    @Autowired
    UserRoleService userRoleService;
    /** 添加Role
     * @param role  前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel addRole(Role role  , String permissionIds) {
        role.setCreateTime(System.currentTimeMillis());
           if(this.save(role )){
               savePrimission(role.getId(),permissionIds);
               return  $.success();
           }else{
                return $.error("添加失败");
           }
    }


    /** 编辑跳转页面的回显数据Role
     * @param id  前端传进的参数
     * @return 返回实体类
     */
    @Override
    public Role selectRoleByIdRole(Integer id ){
            return this.getById(id);
    }


    /** 编辑Role
     * @param role  前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel editRole(Role role ,String permissionIds ) {
           if(this.updateById(role)){
               savePrimission(role.getId(),permissionIds);
               return  $.success();
           }else{
               return $.error("编辑失败");
           }
    }
    /** 删除一条Role
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public   ApiResultModel delRole( Integer id  ){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("role_id",id);
        if(userRoleService.list(queryWrapper).size()>0){
            return $.error("该角色下还有用户，删除失败");
        }
           if(this.removeById(id)){
               Map map=new HashMap();
               map.put("r_id",id);
               rolePermissionService.removeByMap(map);
               return $.success();
           }else{
                return $.error("删除失败");
           }
    }

   /** 分页查询Role
     * @param page  页码
     * @param limit  查几条
     * @param role  前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public ResultPage findRoleByPage(Integer page, Integer limit, Role role){
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("role", role);
        List<Role> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<Role> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    /**
     * 保存角色权限
     * @param roleId
     * @param permissionIds
     */
    private void savePrimission(Integer roleId,String permissionIds){
        Map map=new HashMap();
        map.put("r_id",roleId);
        rolePermissionService.removeByMap(map);
        String[] perIds= StrUtil.split(permissionIds,",");
        for (int i = 0; i < perIds.length; i++) {
            RolePermission rolePermission=new RolePermission();
            rolePermission.setmId(Integer.parseInt(perIds[i]));
            rolePermission.setrId(roleId);
            rolePermissionService.save(rolePermission);
        }
    }
}
